Page({
  data: {
    addressList: [
      {
        name: '张三',
        phone: '13800138000',
        address: '深圳职业技术大学西丽湖校区'
      }
    ],
    showModal: false,
    newName: '',
    newPhone: '',
    newAddress: '',
    editIndex: -1 // 标记当前编辑的地址索引，-1 表示非编辑状态
  },
  showAddAddressModal() {
    this.setData({
      showModal: true,
      editIndex: -1,
      newName: '',
      newPhone: '',
      newAddress: ''
    });
  },
  hideAddAddressModal() {
    this.setData({
      showModal: false,
      newName: '',
      newPhone: '',
      newAddress: '',
      editIndex: -1
    });
  },
  inputName(e) {
    this.setData({
      newName: e.detail.value
    });
  },
  inputPhone(e) {
    this.setData({
      newPhone: e.detail.value
    });
  },
  inputAddress(e) {
    this.setData({
      newAddress: e.detail.value
    });
  },
  editAddress(e) {
    const index = e.currentTarget.dataset.index;
    const { name, phone, address } = this.data.addressList[index];
    this.setData({
      showModal: true,
      editIndex: index,
      newName: name,
      newPhone: phone,
      newAddress: address
    });
  },
  saveAddress() {
    const { newName, newPhone, newAddress, editIndex, addressList } = this.data;
    if (newName && newPhone && newAddress) {
      const newAddressItem = {
        name: newName,
        phone: newPhone,
        address: newAddress
      };
      if (editIndex!== -1) {
        // 编辑地址
        const newAddressList = [...addressList];
        newAddressList[editIndex] = newAddressItem;
        this.setData({
          addressList: newAddressList
        });
      } else {
        // 添加地址
        const newAddressList = [...addressList, newAddressItem];
        this.setData({
          addressList: newAddressList
        });
      }
      this.setData({
        showModal: false,
        newName: '',
        newPhone: '',
        newAddress: '',
        editIndex: -1
      });
    } else {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      });
    }
  },
  deleteAddress(e) {
    const index = e.currentTarget.dataset.index;
    const newAddressList = this.data.addressList.filter((_, i) => i!== index);
    this.setData({
      addressList: newAddressList
    });
  }
});    